package com.sgb.goods.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.contract.api.AbstractCallBack;
import com.sgb.contract.api.ContractNet;
import com.sgb.contract.utils.ArouterConfigs;
import com.sgb.goods.api.GoodsNet;
import com.sgb.goods.entity.ContractInfoEntity;
import com.sgb.goods.utils.base.viewmodel.BaseViewModel;
import com.sgb.goods.view.adapter.ContractInfoListAdapter;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.util.StringUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品订单--合同信息
 * @author cyj
 */
public class ContractListModel extends BaseViewModel {

    private int page = 0;
    public ContractInfoListAdapter adapter;
    public MutableLiveData<List<ContractInfoEntity.Records>> contractRecords = new MutableLiveData<>();
    public MutableLiveData<String> showAlert = new MutableLiveData<>();
    public MutableLiveData<String> showNoContentAlert = new MutableLiveData<>();

    public ContractListModel() {
        adapter = new ContractInfoListAdapter();
    }

    /** 查询合同列表 */
    public void findContractPage(String orderNo, boolean isLoadMore) {
        String companyNo = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO, "");
        Map<String, Object> map = new HashMap<>();
        map.put("companyNo", companyNo);
        map.put("orderNo", orderNo);
        map.put("page", page = isLoadMore ? ++page : 1);
        map.put("limit", 10);
        showLoading();
        GoodsNet.getInstance().getGoodsApi().findContractPage(map).enqueue(new AbstractCallBack<BaseEntity<ContractInfoEntity>>() {
            @Override
            public void onSuccess(BaseEntity<ContractInfoEntity> data) {
                contractRecords.setValue(data.getData().records);
                if(!isLoadMore) {
                    adapter.setNewData(data.getData().records);
                } else {
                    adapter.addData(data.getData().records);
                }
            }
        });
    }

    /** 校验合同 */
    public void checkContract(ContractInfoEntity.Records record) {
        boolean is6or7 = "6".equals(record.status) || "7".equals(record.status);
        String status = is6or7 ? "2" : record.status;
        ContractNet.getInstance().getContractApi().checkStatusByNowStatus(record.no, status, record.appSignStatus, record.appSignTag, record.appAuditStatus)
                .enqueue(new AbstractCallBack<BaseEntity<Boolean>>() {
                    @Override
                    public void onSuccess(BaseEntity<Boolean> data) {
                        if (!is6or7 && "1".equals(record.isTaker) && "1".equals(status) && StringUtil.isEmpty(record.localUrl)) {
                            showNoContentAlert.setValue("any");
                        } else {
                            // 跳转合同详情
                            ARouter.getInstance().build(ArouterConfigs.AR_SIGN_CONTRACT)
                                    // 是否显示合同详情底部操作布局
                                    .withBoolean("isShowOperationLayout", "7".equals(record.status))
                                    // 合同Id
                                    .withString("contractId", record.id)
                                    .navigation();
                        }
                    }

                    @Override
                    public void onBusinessFailure(BaseEntity entity) {
                        if ("539102".equals(entity.getErrCode()) || "539101".equals(entity.getErrCode())) {
                            showAlert.setValue(entity.getMsg());
                        }
                    }
                });
    }

}
